package com.youthHostel.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.PageInfo;
import com.youthHostel.pojo.house;
import com.youthHostel.pojo.lessee;
import com.youthHostel.service.LesseeService;

@Controller
@RequestMapping("/lessee")
@SessionAttributes("lesseeMes")
public class LesseeController {

	@Autowired
	private LesseeService lesseeService;
	
	@RequestMapping("/add")
	public String add() {
		return "addlessee";
	}
	
	@RequestMapping("/addLessee")
	public String addLessee(lessee lessees) {
		lessees.setL_add_time(new Date());
		int rs = lesseeService.addLessee(lessees);
		return "admin";
	}
	
	@RequestMapping("/lesseeList")
	public String lesseeList(Model model,int index) {
		System.out.println("huoseList"+index);
		PageInfo<lessee> lesseeMes = lesseeService.findAlllessee(index);
		model.addAttribute("lesseeMes",lesseeMes);
		return "redirect:/view/lesseelist.jsp";
	}
	
	@RequestMapping("/deleteLesseeById")
	public String deleteLesseeById(int id,HttpServletRequest request) {
		int rs = lesseeService.removeLesseeById(id);
//		if(rs>0) {
		return "redirect:/lessee/lesseeList?index=1";
//		}
//		return "addHouse";
	}
	
	@RequestMapping("/findLesseeMesByid")
	public String findLesseeMesByid(int id,Model model) {
		lessee lessees = lesseeService.findLesseeMesById(id);
		model.addAttribute("lessees",lessees);
		return "editorLessee";
	}
	
	@RequestMapping("/checkLessee")
	public void checkLessee(int id,HttpServletResponse response) {
		lessee lessees = lesseeService.findLesseeMesById(id);
		try {
			if(lessees != null) {
				response.getWriter().write("200");
			}else {
				response.getWriter().write("-1");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/updateLesseeMes")
	public String updateLLesseeMes(lessee lessees,int id) {
		lessees.setL_id(id);
		int rs = lesseeService.updateLesseeMes(lessees);
		System.out.println(rs);
		if(rs > 0) {
			return "admin";
		}
		return "editorLessee";
	}
	
	@RequestMapping("/search")
	public String search(@RequestParam("name")String name,HttpSession session) {
		List<lessee> findHouseByKey = lesseeService.findHouseByKey(name);
		System.out.println(findHouseByKey.size());
		session.removeAttribute("lesseeSearch");
		session.setAttribute("lesseeSearch", findHouseByKey);
		return "redirect:/view/lesseesearch.jsp";
	}
	
}
